// subpkg/reminder/reminder.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    display_delete:'none',
    day:'周日',
    dayOfWeek:1,
    reminderContent:'',
    columns: ['周日','周一', '周二', '周三', '周四', '周五','周六'],
    selectedWeek:'周六',
    minute:'',
    hour:'',
    show: false,
    display_week:'none',
    display:'none',
    currentDate: '12:00',
    minHour: 0,
    maxHour: 24,
    date: '',
    show1: false,
    delete_show:'none',
    addTime:{
      color:'',
      time:'',
      day:'',
      date:'',
      status:false,
      display_delete:'none'
    },

    timeList:
    [
    ]
  },
  solve(){
    console.log(222)
    this.setData({
      display_delete:'none'
    })
  },
  bindinput(e){
    this.setData({
      reminderContent:e.detail.value
    })
  },
  onInput(event) {
    console.log(event.detail)
    var list = event.detail.split(':')
    var hour = parseInt(list[0])
    var minute = parseInt(list[1])
    this.setData({
      hour:hour,
      minute:minute,
      currentDate: event.detail,
    });
  },
  
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show1: false });
  },
  onCancel(){
    this.setData({
      show:false
    })
  },
  onConfirm(){
    wx.request({
      url: 'https://zhixiao.site:8080/alarms/add',
      data:{
        userId:wx.getStorageSync('userid'),
        minute:this.data.minute,
        hour:this.data.hour,
        dayOfWeek:this.data.dayOfWeek,
        reminderContent:this.data.reminderContent
      },
      success:(res)=>{
        console.log(res)
        this.setData({
          show:false
        })
        this.getReminderMsg()
      }
    })
  },
  show_week(){
    this.setData({
      show1:true
    })
  },
  onConfirm1(event){
    var day = event.detail.value
    var index = event.detail.index + 1
    this.setData({
      day:day,
      dayOfWeek:index,
      show1:false
    })
  },
  onCancel1(event){
    this.setData({
      show1:false
    })
  },
  formatDate(date) {
    date = new Date(date)
    console.log(date)
    var date_string = String(date)
    var arr = date_string.split(" ")
    var week = arr[0]
    console.log(date_string)
    console.log(week)
    switch(week){
      case 'Mon': this.setData({selectedWeek:'周一'}) 
       break
      case 'Tue': this.setData({selectedWeek:'周二'})
       break
      case 'Wed': this.setData({selectedWeek:'周三'})
      break
      case 'Thu': this.setData({selectedWeek:'周四'})
      break
      case 'Fri': this.setData({selectedWeek:'周五'})
      break
      case 'Sat': this.setData({selectedWeek:'周六'})
      break
      case 'Sun': this.setData({selectedWeek:'周日'})
      break
    }
    if(date.getMonth()+1 < 10 ){
      if(date.getDate() < 10){
        return `${date.getFullYear()}-0${date.getMonth() + 1}-0${date.getDate()}`;
      }
      return `${date.getFullYear()}-0${date.getMonth() + 1}-${date.getDate()}`;
    }
    else{
      if(date.getDate() < 10){
        return `${date.getFullYear()}-${date.getMonth() + 1}-0${date.getDate()}`;
      }
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    }
    
    
  },
  

  // 删除

  delete_show(e){
    this.setData({
      display_delete:'block'
    })
    console.log(111)
  },
  radioChange(e){
    var index = e.currentTarget.dataset.index
    const timeList = this.data.timeList
    timeList[index].checked = timeList[index].checked?false:true
    this.setData({
      timeList:timeList
    })
    console.log(333)
    

  },

  delete(){
    var ids = []
    var timeList = this.data.timeList
    for(var i = 0;i<timeList.length;i++){
      if(timeList[i].checked){
        ids.push(timeList[i].id)
      }
    }
    var ids = ids.toString()
    this.deleteReminder(ids)
  },
  //***************************************************************** 
  deleteReminder(ids){
    console.log(ids)
    wx.request({
      url: 'https://zhixiao.site:8080/alarms/delete',
      data:{
        ids:ids
      },
      header:{
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        console.log("success")
      },
      complete:(res)=>{
        this.getReminderMsg()
      }
    })
    this.setData({
      display_delete:'none'
    })
    
  },
 //***************************************************************** 




  // 1. add ****************************************
  addReminder(date){
    console.log(date)
    wx.request({
      url: 'https://zhixiao.site:8080/client/add',
      data:{
        // userId:wx.getStorageSync('userid'),
        userId:wx.getStorageSync('userid'),
        notificationTime:date
      },
      header:{
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        console.log("success")
      },
      complete:(res)=>{
        this.getReminderMsg()
      }
    })
    
  },
  //************************************** *
  cancel(){
    this.setData({
      display:'none'
    })
  },
  
  addTime(){
    this.setData({
      display_week:'block'
    })
  },

 //2. update*********************************************************

  onChange(e){
    const index = e.currentTarget.dataset.index
    const timeList = this.data.timeList
    if(this.data.timeList[index].status){
     timeList[index].status = false
     timeList[index].status1 = 0
     timeList[index].color = '#d3d3d3'
    
    }else{
     timeList[index].status = true
     timeList[index].color = '#000'
     timeList[index].status1 = 1
    }
    this.setData({
      timeList:timeList
    })
    wx.request({
      url: 'https://zhixiao.site:8080/alarms/update',
      data:
      {
        id:this.data.timeList[index].id,
        status:this.data.timeList[index].status1
      },
      header:{
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        console.log("success")
      }
    })

  },

  //*****************************************************************
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //获取之前设置过的通话提醒时间 / query
    this.getReminderMsg()
  },
//3. query ***********************************************************
  getReminderMsg(){
    wx.request({
      url: 'https://zhixiao.site:8080/alarms/query',
      data:{
        userId:wx.getStorageSync('userid')
      },
      header:{
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        console.log(res)
        const reminderList = res.data.data
        console.log(reminderList)
        var reminderList1 = []
        for(var i = 0;i < reminderList.length; i++){
          var obj = {}
          var hour = reminderList[i].hour < 10? '0'+reminderList[i].hour:reminderList[i].hour
          var minute = reminderList[i].minute < 10? '0'+reminderList[i].minute:reminderList[i].minute
          var time = hour + ':' + minute
          obj.time = time
          obj.checked = false
          switch (reminderList[i].dayOfWeek){
            case 1:
              obj.dayOfWeek = '周日'
              break
            case 2:
              obj.dayOfWeek = '周一'
              break
            case 3:
                obj.dayOfWeek = '周二'
                break
            case 4:
              obj.dayOfWeek = '周三'
              break  
            case 5:
              obj.dayOfWeek = '周四'
              break
            case 6:
              obj.dayOfWeek = '周五'
              break
            case 7:
              obj.dayOfWeek = '周六'
              break
          }
          obj.id = reminderList[i].id
          if(!reminderList[i].status){
            obj.color = '#d3d3d3'
            obj.status = false
          }else{
            obj.color = '#000'
            obj.status = true
          }
          obj.reminderContent = reminderList[i].reminderContent
          obj.dayOfWeek1 = reminderList[i].dayOfWeek
          obj.minute = reminderList[i].minute
          obj.status1 = reminderList[i].status
          obj.hour = reminderList[i].hour
          obj.display_delete = 'none'
          reminderList1.push(obj)      
        }
        this.setData({
          timeList:reminderList1
        })
       }
     })
  },
 // ***********************************************************

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})